﻿using System;
using System.CodeDom.Compiler;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Microsoft.CSharp;

namespace WinCmdBus
{
	public class DispatchingControllerFactory
	{
		//public static TController CreateController<TController>(Form rootView) where TController : class, IController, new()
		//{
		//    var dispatchServices = new DispatchingServices<TController>();

		//    var proxyGen = new ProxyGenerator();
		//    var baseController = proxyGen.CreateClassProxy<TController>(dispatchServices);

		//    rootView.Load += delegate
		//    {
		//        dispatchServices.PrimeViewCache(rootView.GetType().Assembly);
		//        baseController.DisplayFirstView();
		//    };

		//    return default(TController);
		//}



		//public static T CreateController<T>(Form rootView, params object[] constructorDependencies)
		//{
		//    // override all virtual methods returning ActionResults:
		//    var actionMethods = typeof(T).GetMethods()
		//        .Where(mi => mi.ReturnType == typeof(ActionResult)
		//                     && mi.IsVirtual)
		//        .ToList();

		//    var methodBuilder = new StringBuilder(10000);
					  
		//    foreach (var method in actionMethods)
		//    {
		//        var parameterBuilder = new StringBuilder();
		//        var passParameterBuilder = new StringBuilder();
		//        foreach (var parameter in method.GetParameters())
		//        {
		//            parameterBuilder.AppendFormat("{0} {1},", parameter.ParameterType.FullName, parameter.Name);
		//            passParameterBuilder.Append(parameter.Name).Append(",");

		//        }

		//        if (parameterBuilder.Length > 0)
		//        {
		//            parameterBuilder.Length--;
		//            passParameterBuilder.Length--;
		//        }

		//        methodBuilder.AppendFormat(DispatchMethodTemplate, method.Name, parameterBuilder, passParameterBuilder);
		//    }

		//    var classCode = string.Format(DispatchClassTemplate, typeof(T).Name, methodBuilder, typeof(T).FullName);

		//    var provider = new CSharpCodeProvider();
		//    var parameters = new CompilerParameters();
		//    parameters.OutputAssembly = "Dispatcher" + Guid.NewGuid().ToString("N") + ".dll";
		//    parameters.GenerateInMemory = true;
		//    parameters.GenerateExecutable = false;
		//    parameters.ReferencedAssemblies.Add("Mvc.dll");
		//    parameters.ReferencedAssemblies.Add(Assembly.GetEntryAssembly().CodeBase.Substring(8));
		//    parameters.IncludeDebugInformation = true;

		//    var results = provider.CompileAssemblyFromSource(parameters, classCode);

		//    var compiledController = results.CompiledAssembly.GetTypes().First();

		//    dynamic instance = Activator.CreateInstance(compiledController);
		//    instance.Dispatcher = 

		//    return (T)instance;
		//}

//        static readonly string DispatchMethodTemplate = @"
//		public override ActionResult {0}({1})
//		{{
//			DispatchAction(() =>
//			{{
//				return base.{0}({2});
//			}});
//		}}
//";

//        static readonly string DispatchClassTemplate = @"
//namespace WinCmdBus.Runtime
//{{
//	public class Dispatching{0} : {2}
//	{{
//		public DispatchingController<{2}> Dispatcher {get; set;}
//
//		{1}
//	}}
//}}";
	}
}
